package com.cdvtc.news.web.admin;

import com.cdvtc.news.listener.SessionListener;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin/online")
public class OnlineController {
    @RequestMapping
    public String list(Model model) {
        model.addAttribute("onlineUserMap", SessionListener.onlineUserMap);
        return "admin/online";
    }

    @RequestMapping("offline/{sessionId}")
    public String offline(@PathVariable String sessionId) {

        for(HttpSession session: SessionListener.onlineUserMap.keySet()){
           if(session.getId().equals(sessionId)) {
               session.invalidate(); // 强制会话失效(强退）
               break;
           }
        }
        return "redirect:/admin/online";
    }
}
